﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Flexibles.CApCableRetractSystemAttachmentType1Control
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.Flexibles;

public class CApCableRetractSystemAttachmentType1Control : 
  UserControl,
  ICUiFLEXAttachmentEditControl,
  IComponentConnector
{
  internal Border RetractSystemAttachmentType1Border;
  internal Canvas CanvasRetractSystemAttachmentType1;
  internal Path BlackBoxRetractSystemType1;
  internal Path CableSimulatedSectionDotsRetractSystemType1;
  internal Path CableSimulatedSectionRetractSystemType1;
  internal Canvas StartFrameAxesRetractSystemType1;
  internal TextBlock StartFrameLabelRetractSystemType1;
  internal Canvas StartFramePointerRetractSystemType1;
  internal Canvas EndFrameAxesRetractSystemType1;
  internal Canvas EndFramePointerRetractSystemType1;
  internal TextBlock EndFrameLabelRetractSystemType1;
  internal Canvas MaxRetractedLengthCanvasRetractSystemType1;
  internal TextBlock MaxRetractedLengthLabelRetractSystemType1;
  internal Rectangle MinRetractedLengthLimitRetractSystemType1;
  internal Rectangle MaxRetractedLengthLimitRetractSystemType1;
  internal Rectangle MaxRetractedLengthThrustRetractSystemType1;
  internal Viewbox SpringViewBoxRetractSystemType1;
  internal Rectangle SpringConstantHitBoxRetractSystemType1;
  internal Rectangle MaxRetractedLengthHitBoxRetractSystemType1;
  internal StackPanel RetractSystemAttachmentType1Panel;
  internal Grid RetractSystemAttachmentType1Grid;
  internal TextBlock RetractSystemType1AttachmentFrameLabel;
  internal TxObjEditBoxControl RetractSystemType1AttachmentFrameBoxCtrl;
  internal TextBlock RetractSystemType1DistanceFromStartLabel;
  internal TxNumericEditBoxControl RetractSystemType1DistanceFromStartTxNumericEditBox;
  internal TextBlock RetractSystemType1SpringConstantLabel;
  internal TxNumericEditBoxControl RetractSystemType1SpringConstantTxNumericEditBox;
  internal TextBlock RetractSystemType1MaxRetractedLengthLabel;
  internal TxNumericEditBoxControl RetractSystemType1MaxRetractedLengthTxNumericEditBox;
  private bool _contentLoaded;

  public event ParameterChangeEventHandler ParameterChanged;

  public CApFlexParameterController ParameterController { get; set; }

  public CApCableAttachmentStatusAnalysis CableAttachmentStatusAnalysis { get; set; }

  public TxObjEditBoxControl FrameEditBoxCtrlAttachment
  {
    get => this.RetractSystemType1AttachmentFrameBoxCtrl;
  }

  public ICApFLEXCableAttributeViewModel AttributeViewModel { get; private set; }

  public CApFLEXCableAttachmentViewModel AttachmentViewModel { get; private set; }

  public CApFLEXAttachmentEditorViewModel EditorViewModel { get; }

  public List<Storyboard> AttachmentStoryboards
  {
    get
    {
      return new List<Storyboard>(1)
      {
        this.CanvasRetractSystemAttachmentType1.FindResource((object) "PreviewRetractSystemAttachmentType1") as Storyboard
      };
    }
  }

  public Storyboard ActiveAttachmentStoryboard
  {
    get
    {
      return this.CanvasRetractSystemAttachmentType1.FindResource((object) "PreviewRetractSystemAttachmentType1") as Storyboard;
    }
  }

  public Shape SimulatedShape => (Shape) this.CableSimulatedSectionRetractSystemType1;

  private CApCableAttachmentFrameController AttachmentFrameController { get; set; }

  private bool IsControlLoading => this.EditorViewModel.IsControlLoading;

  private bool IsControlActivated => this.EditorViewModel.IsControlActivated;

  private TxNumericEditBoxControl LastFocusedNumericBox { get; set; }

  public CApCableRetractSystemAttachmentType1Control()
  {
    this.EditorViewModel = new CApFLEXAttachmentEditorViewModel();
  }

  public void InitControl(
    ICApFLEXCableAttributeViewModel attributeViewModel,
    CApFLEXCableAttachmentViewModel attachmentViewModel,
    CApFLEXFrameValidator frameValidator,
    bool isCreationMode,
    CApFlexParameterController parameterController,
    CApCableAttachmentStatusAnalysis cableAttachmentStatusAnalysis,
    CApCableAttachmentFrameController attachmentFrameController)
  {
    this.AttributeViewModel = attributeViewModel;
    this.AttachmentViewModel = attachmentViewModel;
    this.ParameterController = parameterController;
    this.CableAttachmentStatusAnalysis = cableAttachmentStatusAnalysis;
    this.AttachmentFrameController = attachmentFrameController;
    this.InitializeComponent();
    this.InitRetractSystemType1FrameEditBox(frameValidator, isCreationMode);
    this.UpdateLabelText();
    this.SetBindings();
  }

  private void InitRetractSystemType1FrameEditBox(
    CApFLEXFrameValidator frameValidator,
    bool isCreationMode)
  {
    ((UIElement) this.RetractSystemType1AttachmentFrameBoxCtrl).Focusable = false;
    this.RetractSystemType1AttachmentFrameBoxCtrl.ValidatorType = (TxValidatorType) 6;
    this.RetractSystemType1AttachmentFrameBoxCtrl.SetValidator((ITxValidator) frameValidator);
    this.RetractSystemType1AttachmentFrameBoxCtrl.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.RetractSystemType1FrameEditBoxCtrl_Picked);
    if (!isCreationMode)
      this.RetractSystemType1AttachmentFrameBoxCtrl.Object = (ITxObject) this.AttachmentViewModel.FixedFrame;
    ((UIElement) this.RetractSystemType1AttachmentFrameBoxCtrl).KeyUp += new KeyEventHandler(this.RetractSystemAttachmentType1Frame_KeyUp);
  }

  public void SetUpStoryboardsParameters()
  {
    this.ActiveAttachmentStoryboard.SpeedRatio = 1.5;
    this.ActiveAttachmentStoryboard.AutoReverse = true;
  }

  public void UpdateLabelText()
  {
    this.UpdateLinearUnitText();
    this.UpdateConstantSpringLabel();
  }

  public void UpdateLinearUnitText()
  {
    string str1 = CApFLEXStringTable.AttachmentLengthLabel + CApCableUtility.LinearUnitLabelText();
    string str2 = CApFLEXStringTable.AttachmentMaxRetractedLengthLabel + CApCableUtility.LinearUnitLabelText();
    this.RetractSystemType1DistanceFromStartLabel.Text = str1;
    this.RetractSystemType1MaxRetractedLengthLabel.Text = str2;
  }

  public void UpdateConstantSpringLabel()
  {
    TextBlock springConstantLabel = this.RetractSystemType1SpringConstantLabel;
    springConstantLabel.Text = $"{springConstantLabel.Text} ({CApFLEXStringTable.SpringConstantUnitString}):";
    this.RetractSystemType1SpringConstantTxNumericEditBox.DecimalPlaces = 2;
  }

  public void UpdateAttachmentCtrlBox()
  {
    this.RetractSystemType1AttachmentFrameBoxCtrl.Text = "";
    this.RetractSystemType1AttachmentFrameBoxCtrl.Object = (ITxObject) this.AttributeViewModel.StartFrame;
  }

  private void SetBindings()
  {
    Binding binding = new Binding()
    {
      Source = (object) this,
      Path = new PropertyPath("EditorViewModel.IsPreviewIdle", Array.Empty<object>()),
      Converter = (IValueConverter) new BooleanToDiagramVisibilityConverter()
    };
    this.StartFramePointerRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.StartFrameAxesRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.StartFrameLabelRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.EndFramePointerRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.EndFrameAxesRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.EndFrameLabelRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.MaxRetractedLengthCanvasRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.MaxRetractedLengthLabelRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.MaxRetractedLengthHitBoxRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.SpringConstantHitBoxRetractSystemType1.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
  }

  private void AttachmentParentFrameDidChanged(ITxLocatableObject obj)
  {
    this.CableAttachmentStatusAnalysis.CableFrameAttachmentRetriever.UpdateFrameAttachmentRegistry(obj);
  }

  private void DefaultControl_Loaded(object sender, RoutedEventArgs e)
  {
    if (this.IsControlLoading)
      return;
    ((UIElement) (sender as TxNumericEditBoxControl)).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    this.AttachmentViewModel.SpringConstantSelected = true;
  }

  private void SpringConstant_DiagramFocus(object sender, MouseButtonEventArgs e)
  {
    ((UIElement) this.RetractSystemType1SpringConstantTxNumericEditBox).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  }

  private void SpringConstant_GotFocus(object sender, RoutedEventArgs e)
  {
    this.DoSetDefaultFocus();
  }

  private void SpringConstant_LostFocus(object sender, RoutedEventArgs e)
  {
    this.AttachmentViewModel.SpringConstantSelected = false;
  }

  private void MaxRetractedLength_DiagramFocus(object sender, MouseButtonEventArgs e)
  {
    ((UIElement) this.RetractSystemType1MaxRetractedLengthTxNumericEditBox).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  }

  private void MaxRetractedLength_GotFocus(object sender, RoutedEventArgs e)
  {
    this.AttachmentViewModel.MaxRetractedLengthSelected = true;
  }

  private void MaxRetractedLength_LostFocus(object sender, RoutedEventArgs e)
  {
    this.AttachmentViewModel.MaxRetractedLengthSelected = false;
  }

  private void RetractSystemAttachmentType1Frame_KeyUp(object sender, KeyEventArgs e)
  {
    if (e.Key != Key.Back && e.Key != Key.Delete)
      return;
    this.AttachmentParentFrameDidChanged(this.AttachmentViewModel.FixedFrame);
    CApCableUtility.UnHighlightObject((ITxDisplayableObject) (this.AttachmentViewModel.FixedFrame as TxFrame));
    this.AttachmentViewModel.FixedFrame = (ITxLocatableObject) null;
    this.RetractSystemType1AttachmentFrameBoxCtrl.Text = "";
  }

  private void RetractSystemType1FrameEditBoxCtrl_Picked(
    object sender,
    TxObjEditBoxCtrl_PickedEventArgs args)
  {
    TxFrame selectedFrame = this.RetractSystemType1AttachmentFrameBoxCtrl.Object as TxFrame;
    ITxLocatableObject fixedFrame = this.AttachmentViewModel.FixedFrame;
    this.AttachmentFrameController.FrameEditorPicked(selectedFrame, ref fixedFrame, this.RetractSystemType1AttachmentFrameBoxCtrl.Text);
    this.AttachmentViewModel.FixedFrame = fixedFrame;
  }

  public void UpdateAttachmentParametersWithParameters()
  {
    if (!this.IsControlActivated)
      return;
    this.AttachmentViewModel.Location = CApFLEXAttachmentLocation.Start;
    this.AttachmentViewModel.FixedFrame = this.AttributeViewModel.StartFrame;
    this.AttachmentViewModel.SecondaryFixedFrame = (ITxLocatableObject) null;
    this.AttachmentViewModel.LengthFromStart = 0.0;
    this.AttachmentViewModel.SpringConstant = this.ParameterController.RetractSystemSpringConstant;
    this.AttachmentViewModel.MaxRetractedLength = this.AttributeViewModel.Length * 0.5;
  }

  public void UpdateAttachmentParametersWithAttachment(CApFLEXAttachment attachment)
  {
    if (!this.IsControlActivated)
      return;
    this.AttachmentViewModel.AttachmentType = attachment.AttachmentType;
    this.AttachmentViewModel.Location = attachment.Location;
    this.AttachmentViewModel.FixedFrame = attachment.FixedFrame;
    this.AttachmentViewModel.SecondaryFixedFrame = (ITxLocatableObject) null;
    this.AttachmentViewModel.LengthFromStart = attachment.LengthFromStart;
    CApFLEXRetractSystemAttachmentType1 systemAttachmentType1 = attachment as CApFLEXRetractSystemAttachmentType1;
    this.AttachmentViewModel.SpringConstant = systemAttachmentType1.SpringConstant;
    this.AttachmentViewModel.MaxRetractedLength = systemAttachmentType1.MaxRetractedLength;
  }

  public void SetDefaultFocus()
  {
    if (!this.IsControlActivated)
      return;
    ((UIElement) this.RetractSystemType1SpringConstantTxNumericEditBox).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    this.DoSetDefaultFocus();
  }

  private void DoSetDefaultFocus() => this.AttachmentViewModel.SpringConstantSelected = true;

  public void SetEnabledAllParametersControls(bool isEnabled)
  {
    if (!isEnabled)
      this.LastFocusedNumericBox = this.GetCurrentlyFocusedNumericBox();
    this.RetractSystemAttachmentType1Panel.IsEnabled = isEnabled;
    if (!isEnabled || this.LastFocusedNumericBox == null)
      return;
    ((UIElement) this.LastFocusedNumericBox).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    this.LastFocusedNumericBox = (TxNumericEditBoxControl) null;
  }

  private TxNumericEditBoxControl GetCurrentlyFocusedNumericBox()
  {
    TxNumericEditBoxControl focusedNumericBox = (TxNumericEditBoxControl) null;
    if (this.AttachmentViewModel.SpringConstantSelected)
      focusedNumericBox = this.RetractSystemType1SpringConstantTxNumericEditBox;
    else if (this.AttachmentViewModel.MaxRetractedLengthSelected)
      focusedNumericBox = this.RetractSystemType1MaxRetractedLengthTxNumericEditBox;
    return focusedNumericBox;
  }

  public void DidDialogClose()
  {
    this.RetractSystemType1AttachmentFrameBoxCtrl.Picked -= new TxObjEditBoxCtrl_PickedEventHandler(this.RetractSystemType1FrameEditBoxCtrl_Picked);
    ((UIElement) this.RetractSystemType1AttachmentFrameBoxCtrl).KeyUp -= new KeyEventHandler(this.RetractSystemAttachmentType1Frame_KeyUp);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/flexibles/attachmentcontrols/capcableretractsystemattachmenttype1control.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.RetractSystemAttachmentType1Border = (Border) target;
        break;
      case 2:
        this.CanvasRetractSystemAttachmentType1 = (Canvas) target;
        break;
      case 3:
        this.BlackBoxRetractSystemType1 = (Path) target;
        break;
      case 4:
        this.CableSimulatedSectionDotsRetractSystemType1 = (Path) target;
        break;
      case 5:
        this.CableSimulatedSectionRetractSystemType1 = (Path) target;
        break;
      case 6:
        this.StartFrameAxesRetractSystemType1 = (Canvas) target;
        break;
      case 7:
        this.StartFrameLabelRetractSystemType1 = (TextBlock) target;
        break;
      case 8:
        this.StartFramePointerRetractSystemType1 = (Canvas) target;
        break;
      case 9:
        this.EndFrameAxesRetractSystemType1 = (Canvas) target;
        break;
      case 10:
        this.EndFramePointerRetractSystemType1 = (Canvas) target;
        break;
      case 11:
        this.EndFrameLabelRetractSystemType1 = (TextBlock) target;
        break;
      case 12:
        this.MaxRetractedLengthCanvasRetractSystemType1 = (Canvas) target;
        break;
      case 13:
        this.MaxRetractedLengthLabelRetractSystemType1 = (TextBlock) target;
        break;
      case 14:
        this.MinRetractedLengthLimitRetractSystemType1 = (Rectangle) target;
        break;
      case 15:
        this.MaxRetractedLengthLimitRetractSystemType1 = (Rectangle) target;
        break;
      case 16 /*0x10*/:
        this.MaxRetractedLengthThrustRetractSystemType1 = (Rectangle) target;
        break;
      case 17:
        this.SpringViewBoxRetractSystemType1 = (Viewbox) target;
        break;
      case 18:
        this.SpringConstantHitBoxRetractSystemType1 = (Rectangle) target;
        this.SpringConstantHitBoxRetractSystemType1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.SpringConstant_DiagramFocus);
        break;
      case 19:
        this.MaxRetractedLengthHitBoxRetractSystemType1 = (Rectangle) target;
        this.MaxRetractedLengthHitBoxRetractSystemType1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.MaxRetractedLength_DiagramFocus);
        break;
      case 20:
        this.RetractSystemAttachmentType1Panel = (StackPanel) target;
        break;
      case 21:
        this.RetractSystemAttachmentType1Grid = (Grid) target;
        break;
      case 22:
        this.RetractSystemType1AttachmentFrameLabel = (TextBlock) target;
        break;
      case 23:
        this.RetractSystemType1AttachmentFrameBoxCtrl = (TxObjEditBoxControl) target;
        break;
      case 24:
        this.RetractSystemType1DistanceFromStartLabel = (TextBlock) target;
        break;
      case 25:
        this.RetractSystemType1DistanceFromStartTxNumericEditBox = (TxNumericEditBoxControl) target;
        break;
      case 26:
        this.RetractSystemType1SpringConstantLabel = (TextBlock) target;
        break;
      case 27:
        this.RetractSystemType1SpringConstantTxNumericEditBox = (TxNumericEditBoxControl) target;
        ((UIElement) this.RetractSystemType1SpringConstantTxNumericEditBox).GotFocus += new RoutedEventHandler(this.SpringConstant_GotFocus);
        ((UIElement) this.RetractSystemType1SpringConstantTxNumericEditBox).LostFocus += new RoutedEventHandler(this.SpringConstant_LostFocus);
        ((FrameworkElement) this.RetractSystemType1SpringConstantTxNumericEditBox).Loaded += new RoutedEventHandler(this.DefaultControl_Loaded);
        break;
      case 28:
        this.RetractSystemType1MaxRetractedLengthLabel = (TextBlock) target;
        break;
      case 29:
        this.RetractSystemType1MaxRetractedLengthTxNumericEditBox = (TxNumericEditBoxControl) target;
        ((UIElement) this.RetractSystemType1MaxRetractedLengthTxNumericEditBox).GotFocus += new RoutedEventHandler(this.MaxRetractedLength_GotFocus);
        ((UIElement) this.RetractSystemType1MaxRetractedLengthTxNumericEditBox).LostFocus += new RoutedEventHandler(this.MaxRetractedLength_LostFocus);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
